<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>推荐位管理</el-breadcrumb-item>
        <el-breadcrumb-item>分类书籍模块内容配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <!--自定义表单 -->
        <template slot-scope="scope" slot="category_1Form">
          <el-select v-model="form.category_1" placeholder="请选择分类">
            <el-option
              v-for="e in categoryList.filter((e) => e.site == site)"
              :key="e.category_id"
              :label="e.name"
              :value="e.category_id"
            >
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="category_2Form">
          <el-select v-model="form.category_2" placeholder="请选择分类">
            <el-option
              v-for="e in categoryList.filter((e) => e.site == site)"
              :key="e.category_id"
              :label="e.name"
              :value="e.category_id"
            >
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="category_3Form">
          <el-select v-model="form.category_3" placeholder="请选择分类">
            <el-option
              v-for="e in categoryList.filter((e) => e.site == site)"
              :key="e.category_id"
              :label="e.name"
              :value="e.category_id"
            >
            </el-option>
          </el-select>
        </template>
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>


<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  appConfigCategoryBookRecommend as baseURL,
  bookCategory
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      data: [],
      params: {},
      form: {},
      categoryList: [],
      site: 1,
      message: "",
      default: {
        orderByList: "create_time&ASC",
      },
      option: {
        border: true,
        selection: true,
        border: true,
        align: "center",
        headerAlign: "center",
        index: true,
        indexLabel: "编号",
        column: [
          {
            label: "模块名称",
            prop: "m_id",
            type: "select",
            search: true,
            row: true,
            rules: [
              {
                required: true,
                message: "模块名称必须填写",
              },
            ],
            placeholder: "请选择模块名称",
            dicData: [
              {
                label: "分类精选",
                value: 45,
              },
              {
                label: "分类标题",
                value: 46,
              },
            ],
          },
          {
            label: "偏好选择",
            prop: "read_preference",
            type: "select",
            search: true,
            rules: [
              {
                required: true,
                message: "偏好选择必须填写",
              },
            ],
            placeholder: "请选择偏好选择",
            value: 1,
            dicData: [
              {
                label: "男",
                value: 1,
              },
              {
                label: "女",
                value: 2,
              },
            ],
            change: (res) => {
              if (res.value != "") {
                this.site = res.value;
              }
              this.form.category_1 = "";
              this.form.category_2 = "";
              this.form.category_3 = "";
            },
          },
          {
            label: "备注",
            prop: "info",
            type: "input",
            placeholder: "请填写备注",
            rules: [
              {
                required: true,
                message: "备注必须填写",
              },
            ],
          },
          {
            label: "分类1",
            prop: "category_1",
            type: "select",
            placeholder: "请选择分类1",
            dicUrl: `${catUrl}/${bookCategory.selectByPreference}/0`,
            formslot: true,
            props: {
              label: "name",
              value: "category_id",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "分类1必须填写",
              },
            ],
          },
          {
            label: "分类1书籍",
            prop: "book_ids_1",
            type: "textarea",
            span: 12,
            placeholder: "请填写书籍id配置(多id间用英文,号隔开 或 一行一个)",
            rules: [
              {
                required: true,
                message: "书籍id配置必须填写",
              },
            ],
          },
          {
            label: "分类2",
            prop: "category_2",
            type: "select",
            placeholder: "请选择分类1",
            dicUrl: `${catUrl}/${bookCategory.selectByPreference}/0`,
            formslot: true,
            props: {
              label: "name",
              value: "category_id",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "分类2必须填写",
              },
            ],
          },
          {
            label: "分类2书籍",
            prop: "book_ids_2",
            type: "textarea",
            span: 12,
            placeholder: "请填写书籍id配置(多id间用英文,号隔开 或 一行一个)",
            rules: [
              {
                required: true,
                message: "书籍id配置必须填写",
              },
            ],
          },
          {
            label: "分类3",
            prop: "category_3",
            type: "select",
            placeholder: "请选择分类1",
            dicUrl: `${catUrl}/${bookCategory.selectByPreference}/0`,
            formslot: true,
            props: {
              label: "name",
              value: "category_id",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "分类3必须填写",
              },
            ],
          },
          {
            label: "分类3书籍",
            prop: "book_ids_3",
            type: "textarea",
            span: 12,
            placeholder: "请填写书籍id配置(多id间用英文,号隔开 或 一行一个)",
            rules: [
              {
                required: true,
                message: "书籍id配置必须填写",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
          {
            label: "书籍类型",
            prop: "book_type",
            type: "input",
            value: 1,
            hide: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.pageAndParam}`,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      response.data.forEach((e) => {
        e.book_ids_1 = e.book_ids_1.join(",");
        e.book_ids_2 = e.book_ids_2.join(",");
        e.book_ids_3 = e.book_ids_3.join(",");
      });
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];

      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.book_ids_1 = row.book_ids_1.replace(/\r\n|\n/g,",").split(",").map((e) => e.trim());
      row.book_ids_2 = row.book_ids_2.replace(/\r\n|\n/g,",").split(",").map((e) => e.trim());
      row.book_ids_3 = row.book_ids_3.replace(/\r\n|\n/g,",").split(",").map((e) => e.trim());
      let data = await common(`${baseURL.save}`, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }

      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.book_ids_1 = row.book_ids_1.replace(/\r\n|\n/g,",").split(",").map((e) => e.trim());
      row.book_ids_2 = row.book_ids_2.replace(/\r\n|\n/g,",").split(",").map((e) => e.trim());
      row.book_ids_3 = row.book_ids_3.replace(/\r\n|\n/g,",").split(",").map((e) => e.trim());
      let data = await common(`${baseURL.update}`, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }

      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseURL.delete}`,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    async initCategoryList() {
      let data = await common(
        `${bookCategory.selectByPreference}/0`,
        null,
        null,
        "GET"
      );
      if (data.code == 1) {
        this.categoryList = data.data;
      }
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      if (this.params.m_name) {
        this.params.m_name = "%" + this.params.m_name + "%";
      }
      this.getList();
      if (done) done();
    },
  },
  created() {
    this.initCategoryList();
  },
};
</script>


